package com.ch.forum.utils;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import java.text.SimpleDateFormat;
import java.util.Date;

/** 时间工具类
 * Created by iceyohoo on 2016-08-11.
 */
public class DateUtils {

    private DateUtils(){}

    /**
     * 格式化时间
     * @param time
     * @param format
     * @return
     */
    public static Date getFormatTimeDate(String time, String format) {

        if (StringUtils.isEmpty(time)) {
            return null;
        }

        DateTime dateTime = null;
        DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(format);
        try {
            dateTime = DateTime.parse(time, dateTimeFormatter);
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (dateTime != null) {
            return dateTime.toDate();
        }

        return null;
    }

    /**
     * 格式化时间
     * @param time
     * @param format
     * @return
     */
    public static String getFormatTimeStr(Date time, String format) {

        if (time == null) {
            return null;
        }

        DateTime dateTime = new DateTime(time);

        return dateTime.toString(format);
    }

    /**
     * 格式化时间
     * @param time
     * @param format
     * @return
     */
    public static String getFormatTimeStr(long time, String format) {

        DateTime dateTime = new DateTime(time);

        return dateTime.toString(format);
    }

    /**
     * 获得当前时间
     * @param format 时间格式
     * @return
     */
    public static String getCurrentTime(String format){
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        if(StringUtils.isEmpty(format)){
            return "";
        }
        return sdf.format(d);
    }

    /**
     * 比较两个日期的大小，d1是否大于d2
     * @param d1
     * @param d2
     * @param format
     * @return
     */
    public static boolean d1MoreD2(String d1, String d2, String format){

        SimpleDateFormat sdf = new SimpleDateFormat(format);
        boolean isMore = false;
        try {
            Date data1 = sdf.parse(d1);
            Date data2 = sdf.parse(d2);
            isMore = data1.before(data2);
            if(isMore){
                return false;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return true;
    }

    /**
     * 格式化微信返回时间格式
     * @param time
     * @return
     */
    public static String formatPayTime(String time){

        StringBuilder sb = new StringBuilder();

        String year = time.substring(0,4);
        String month = time.substring(4,6);
        String day = time.substring(6,8);
        String hh = time.substring(8,10);
        String mm = time.substring(10,12);
        String ss = time.substring(12,14);


        sb.append(year).append("-");
        sb.append(month).append("-");
        sb.append(day);

        sb.append(" ");
        sb.append(hh).append(":");
        sb.append(mm).append(":");
        sb.append(ss);

        return sb.toString();
    }

    /**
     * 格式化返回数据
     * @param time
     * @return
     */
    public static String formatRtTime(String time){

        StringBuilder sb = new StringBuilder();

        String year = time.substring(0,4);
        String month = time.substring(4,6);
        String day = time.substring(6,8);

        sb.append(year).append("年");
        sb.append(month).append("月");
        sb.append(day).append("日");

        return sb.toString();
    }

}
